//
//  PersonVC.m
//  EJLX
//
//  Created by adc on 15/11/9.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PersonVC.h"
//#import "PeopleCellSetting.h"
#import "ChangeThePasswordVC.h"
#import "CheckPeopleVC.h"
#import "HomeVC.h"
#import "GoodsAdressVC.h"
#import "AboutOur.h"
#import "LoginVC.h"
@interface PersonVC ()

@end

@implementation PersonVC
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    //NSString*name,*avatar,*levle,*status;//姓名，图片，级别，状态
    [HttpsManager requestJianJieXinXiCompletion:^(XinXiJianJie *xinXi) {
        self.user = xinXi;
        [_tableView reloadData];
    }];
    

}

-(void)backBtn{
        [self.navigationController popViewControllerAnimated:YES];
}
-(void)creatNav{
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"个人中心";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self creatNav];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titleArray =@[@"个人资料",@"收货地址",@"修改密码",@"关于我们",@"客服中心",@"用户协议",@"升级软件"];
    self.imgArr = @[@"个人资料新",@"收货地址新",@"修改密码新",@"关于我们新",@"客服中心新",@"用户协议新",@"升级软件新"];
    self.navigationController.navigationBarHidden = NO;
    self.title = @"个人中心";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDE, HEIGHT-56-64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
   // [_tableView setScrollEnabled:NO];
    _tableView.tableFooterView =[[UIView alloc] init];
    [self.view addSubview:_tableView];
    [HttpsManager requestJianJieXinXiCompletion:^(XinXiJianJie *xinXi) {
//       UIView *headVC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDE, 180)];
//        headVC.backgroundColor = RGB_COLOR(0x00c49f);
//        
//        _peopleImage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDE/2-40, 20, 80, 80)];
//        _peopleImage.layer.cornerRadius = 40;
//        [_peopleImage sd_setImageWithURL:[NSURL URLWithString:xinXi.avatar] placeholderImage:[UIImage imageNamed:@"头像"]];
//        _peopleImage.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap )];
//        [_peopleImage addGestureRecognizer:tap];
//       
//        [headVC addSubview:_peopleImage];
//        
//        _userNameLB = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-80, 100, 160, 20 )];
//        _userNameLB.text = xinXi.name;
//        _userNameLB.textColor = [UIColor whiteColor];
//        _userNameLB.textAlignment = NSTextAlignmentCenter;
//        
//        [headVC addSubview:_userNameLB];
//        
//        UILabel *userLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 140, WIDE, 20 )];
//        
//        userLB.text = [NSString stringWithFormat:@"级别:%@｜审核状态:%@",xinXi.levle,xinXi.status];
//        userLB.font = [UIFont systemFontOfSize:12.0f];
//        userLB.textColor = [UIColor whiteColor];
//        
//        userLB.textAlignment = NSTextAlignmentCenter;
//        [headVC addSubview:userLB];
//        _tableView.tableHeaderView = headVC;
        
        [_tableView reloadData];
    }];

         [self addExitBtn];
    
    
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}
-(void)addExitBtn{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake( 100, HEIGHT-50, WIDE-200, 30);
    [btn setTitle:@"退出当前账号" forState:UIControlStateNormal];
    btn.backgroundColor = RGB_COLOR(0x00c49f);
    [btn addTarget:self action:@selector(exitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)exitBtnClick{
    
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud removeObjectForKey:@"token"];
    //[ud removeObjectForKey:@"date"];
  //  [ud removeObjectForKey:@"name"];
    [ud synchronize];
    
    LoginVC *login = [[LoginVC alloc] init];
    [self.navigationController pushViewController:login animated:YES];
//    HomeVC*vc=[[HomeVC alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
  
}
-(void)tap{
    [self changeImageForPeople];
}
-(void)changeImageForPeople{
    //转改头像
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [sheet addAction:[UIAlertAction actionWithTitle:@"📷" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePicker = [[UIImagePickerController alloc] init];
        
        _imagePicker.delegate = self;
        
     _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
       _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        _imagePicker.allowsEditing = YES;
        
      [self presentViewController:_imagePicker animated:YES completion:nil];
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePicker = [[UIImagePickerController alloc] init];
        
       _imagePicker.delegate = self;
        
      _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
      _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
      _imagePicker.allowsEditing = YES;
        
      [self presentViewController:_imagePicker animated:YES completion:nil];
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:sheet completion:nil];
    }]];
    [self presentViewController:sheet animated:YES completion:nil];
    
}

#pragma mark - picker
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//   
//   UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//    
//    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){
//            
//       // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//            
//           }
    
    
    UIImage * image = info[UIImagePickerControllerOriginalImage];
    _peopleImage.image = image;
    
    [HttpsManager postimage:image block:^(NSString *err) {
        [GMDCircleLoader setOnView:self.view withTitle:@"上传中.........." animated:YES];
        SHOW_ALERT(err);
        if ([err isEqualToString:@"ok"]) {
            [_tableView reloadData];
            [GMDCircleLoader hideFromView:self.view animated:YES];
        }
    }];
    [self dismissViewControllerAnimated:YES completion:nil];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];
   
    cell.imageView.image = [UIImage imageNamed:self.imgArr[indexPath.row]];
    if (indexPath.row == self.titleArray.count) {
        cell.detailTextLabel.text = @"_dp";
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //[@"个人资料",@"收货地址",@"修改密码",@"关于我们",@"客服中心",@"用户协议",@"升级软件"]
    if (indexPath.row ==0) {
        //个人资料
        CheckPeopleVC *check = [[CheckPeopleVC alloc] init];
        [self.navigationController pushViewController:check animated:YES];
        
    }else if (indexPath.row == 1){
        //收货地址
        GoodsAdressVC *vc = [[GoodsAdressVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 2){
        //修改密码
        ChangeThePasswordVC *changePsd = [[ChangeThePasswordVC alloc] init];
        
        [self.navigationController pushViewController:changePsd animated:YES];

    }
    else if (indexPath.row == 3){
            //关于我们
        AboutOur *our = [[AboutOur alloc] init];
        [self.navigationController pushViewController:our animated:YES];
    }else if (indexPath.row == 4){
        //客服中心
        
        
        UIWebView *web = [[UIWebView alloc] init];
        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://400-86880008"]]];
        [self.view addSubview:web];

    }
    else if (indexPath.row == 5){
        //用户协议
        GoToWebView *web = [[GoToWebView alloc] initWithurl:[NSURL URLWithString:[NSString stringWithFormat:@"%@/comm/user_agreement",HTTPS_ID]] title:@"用户协议"];
        [self.navigationController pushViewController:web animated:YES];
        
    }else if (indexPath.row ==6){
        //软件更新
        
        [HttpsManager requsetGetVersionForAPPBlock:^(NSDictionary *dict) {
            NSString *version = [[dict objectForKey:@"data"] objectForKey:@"v_name"];
            
            NSString *currentVersion = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
            
            NSLog(@"current %@ \n version %@",currentVersion,version);
            if (version == NULL) {
                SHOW_ALERT(@"当前已是最新版本")
            }
            if (version>currentVersion) {
                NSString *str = [NSString stringWithFormat:@"发现新版本(%@),是否更新?",version];
                SHOW_ALERT(str)
                //点击更新
                
            }
        }];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
